package com.qingke.besuper.command.impl;

import org.apache.commons.codec.digest.DigestUtils;

import com.qingke.besuper.app.BeSuperApp;
import com.qingke.besuper.command.SystemCommand;
import com.qingke.besuper.pojo.User;
import com.qingke.besuper.util.BeSuperConsole;

public class LoginCommand extends SystemCommand {
	private User user=null;
	@Override
	public void execute() {
		BeSuperConsole.println();
		BeSuperConsole.println("----------------");
		String username=BeSuperConsole.promptNotEmpty("username:");
		String password=BeSuperConsole.promptNotEmpty("password:");
		BeSuperConsole.println("----------------");
		
		if(!dao.isUsernameExists(username)){
			BeSuperConsole.println("The user does not exist");
			return ;
		}
		
		user=dao.login(username, DigestUtils.md5Hex(password));
		
		if(user!=null){
			BeSuperApp.getInstance().setUser(user);
			BeSuperConsole.println("Login success.");
			BeSuperConsole.println();
		}
		else{
			BeSuperConsole.println("Login failed , password is not correct");
			BeSuperConsole.println();
		}
	}
	public User getUser(){
		return user;
	}

}
